from constants import *
from Tkinter import *
from elements import TilePart, Tile, Feature, Follower, Board
from game import Game

# current view state
IMAGES = 'Images'
GRID = 'Grid'

class GUI:
    '''
    inputs: 
        None
    defs:
        createWidgets
        createBoardFrame
        newGame
    '''    
    def __init__(self):
        # constructor
        # returns: None
        # TODO: add score track, piece display, and current player
        root = Tk()
        
        self.createWidgets()
        
        boardFrame = Frame(root)
        
        self.boardWidget = self.createBoardFrame(boardFrame)
        self.menu = self.createMenu(root)
        
        boardFrame.grid()
        
        root.mainloop()
        
    def createWidgets(self):
        # initializes widgets and game constants
        # returns: None
        self.game = None
        self.boardWidget = None
        self.boardWidgetHeight = 500
        self.boardWidgetWidth = 500
        self.currentView = IMAGES
        #TODO: import tile images
        
    def createBoardFrame(self, frame):
        # creates board widget in the frame widget
        # returns: Canvas object packed in the frame widget
        widget = Canvas(frame, height = self.boardWidgetHeight, width = self.boardWidgetWidth)
        # TODO: create boad bindings
        widget.pack(fill = 'both')
        return widget
    
    def createMenu(self, root):
        # creates menubar for the root widget
        # returns: Menu object attached to the root widget
        menubar = Menu(root)
        filemenu = Menu(menubar, tearoff = 0)
        filemenu.add_command(label = "New Game", command = self.askNewGame, accelerator="Ctrl+N")
        filemenu.add_command(label = 'Toggle Grid View', command = self.toggleView, accelerator="Ctrl+G")
        filemenu.add_separator()
        filemenu.add_command(label = 'Exit', command = root.quit, accelerator="Ctrl+Q")
        helpmenu = Menu(menubar, tearoff = 0)
        helpmenu.add_command(label = 'How To Play...', command = self.showHelp)
        helpmenu.add_command(label = 'About', command = self.showAbout)
        menubar.add_cascade(label = 'File', menu = filemenu)
        menubar.add_cascade(label = 'Help', menu = helpmenu)
        root.config(menu=menubar)
        return menubar
    
    def askNewGame(self):
        # Prompts user with new game dialog
        # returns: None
        top = Toplevel()
        ruleVar = StringVar()
        ruleFrame = Frame(top)
        playerFrame = Frame(top)
        buttonFrame = Frame(top)
        Label(ruleFrame, text = 'Choose what tile sets to play with').grid(sticky = 'W', row = 0, column = 0)
        i = 1
        for rule in AVAILABLE_SETS.keys():
            Radiobutton(ruleFrame, text = rule, variable = ruleVar, value = rule, command = self.updateNewGameParams).grid(sticky = 'W', row = i, column = 0)
            i = i + 1
        s = Spinbox(playerFrame, values = )
        
        
        string = 'Start a new game.'
        if self.game != None:
            string = string + '\nNote: Starting a new game will erase current progress'
        Label(top, text = string).grid(row = 0, column = 0, columnspan = 2)
        ruleFrame.grid(row = 1, column = 0)
        playerFrame.grid(row = 1, column = 1)
        buttonFrame.grid(row = 2, column = 0, columnspan = 2)
    
    def updateNewGameParams(self):
        # updates radio buttons and spinboxes
        # returns: None
        #TODO: add this logic
        pass
        
    
    def toggleView(self):
        # toggles between images and raw tile grid
        # returns: None
        #TODO: add this logic
        pass
    
    def showHelp(self):
        # displays game help in new topLevel
        # returns: None
        #TODO: add this logic
        pass
    
    def showAbout(self):
        # displays program information in new topLevel
        # returns: None
        #TODO: add this logic
        pass
    
    def newGame(self, startingPlayer, rules):
        self.game = Game(startingPlayer, rules)
    
if __name__ == '__main__':
    gui = GUI()